package mediator;

import java.sql.Connection;
import java.sql.ResultSet;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;

import entity.Taxi.Status;

import entity.*;
import foundation.*;

/**
 * 
 * @uml.dependency supplier="foundation.Connection"
 * @author kasper
 */
public class MBroker {

	private static Connection readcon = null;
	private static Connection writecon = null;
	private static FWrite write = null;
	private static FRead read = null;
	private static MBroker aBroker = new MBroker();

	public static MBroker getInstance() {
		return aBroker;
	}

	/**
	 * Metoden opretter forbindelse til databasen, og får Write til at gemme
	 * positionen i databasen.
	 */

	public MBroker() {
		FConnection db = FConnection.getConnection();
		try {
			writecon = db.dbConnect("root", "danni"); // Gives connection to
			// the database with
			// read/write access
			readcon = db.dbConnect("root", "danni"); // Gives connection to
			// the db with read
			// access
		} catch (Exception e) {
			System.out.println(e.getMessage());
		} // TODO username and password should be arguments
		write = FWrite.getInstance();
		read = FRead.getInstance();
	}

	public void dematerializePosition(entity.Taxi aTaxi) {
		entity.Position aPosition = aTaxi.getPosition();
		long time = aPosition.getTime().getTimeInMillis();
		float longitude = aPosition.getLongitude();
		float latitude = aPosition.getLatitude();
		String status = aTaxi.getStatus().toString();
		int taxiID = aTaxi.getTaxiID();
		write.dematerializePosition(writecon, time, longitude, latitude,
				status, taxiID);
	}

	public void dematerializeOrder(entity.Order aOrder) {
		String address = aOrder.getAddress();
		String city = aOrder.getCity();
		String zipCode = aOrder.getZipCode();
		String phone = aOrder.getPhone();
		int orderID = aOrder.getOrderid();
		long ordertime = aOrder.getDate();
		System.out.println("Order: " + address + ", " + city + ", " + zipCode
				+ ", " + phone + " " + orderID + " " + ordertime);

		write.dematerializeOrder(writecon, address, city, zipCode, phone,
				orderID, ordertime);
	}

	/**
	 * this method counts the amount of orders that have been placed.
	 * 
	 * @return
	 */
	public synchronized int orderCount() {
		int counter = 0;
		counter = read.orderCount(readcon);
		return counter;
	}

	public void materializeAllTaxisWithLatestPosition() {
		try {
			Calendar time = new GregorianCalendar();

			ResultSet rsLatestPos = null;
			ResultSet rsTaxis = read.getAllTaxis(readcon);
			Status status = null;

			while (rsTaxis.next()) {

				int taxiID = rsTaxis.getInt("taxiID");

				rsLatestPos = read.getLatestPosForTaxi(readcon, taxiID);
				rsLatestPos.next();

				time.setTimeInMillis((long) rsLatestPos.getLong("time"));
				float longitude = rsLatestPos.getFloat("longitude");
				float latitude = rsLatestPos.getFloat("latitude");
				// status = status.valueOf(rsLatestPos.getString("status"));
				// //taxaer initialiseres til sidste tilgængelige status
				status = Status.offline; // alle taxaer initialiseres til
				// offline

				Taxi tempTaxa = new Taxi(taxiID, longitude, latitude, time,
						status);
				Taxi.addTaxi(tempTaxa);

			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public List<Float> makeStatistics(int weeks, long chosenDate, int zipCodeFrom, int zipCodeTo) throws Exception {
			
		return read.getStatistics(readcon,weeks,chosenDate,zipCodeFrom,zipCodeTo);
	}
//	public static void main(String[] args) throws Exception {
//		System.out.println(MBroker.getInstance().makeStatistics(Calendar.MONDAY,12,"5000"));
//	}
}
